"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClubTopDao = void 0;
const ClubConfig_1 = require("../../hall_server/config/ClubConfig");
const DbConfig_1 = require("../../src/config/DbConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
class ClubTopDao extends DaoBase_1.DaoBase {
    static insertTop(param) {
        return __awaiter(this, void 0, void 0, function* () {
            let clubid = param["clubid"];
            let userid = param["userid"];
            let score = param["score"];
            let wintimes = param["wintimes"];
            let gametimes = param["gametimes"];
            let dateStr = DateUtil_1.default.formatDateStr3(DateUtil_1.default.now());
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from t_club_top where `clubid`=? and `userid`=? and `date`=?";
            let sqlParams = [clubid, userid, dateStr];
            let rows = yield client.queryPromise(sql, sqlParams);
            // Logger.log("insertTop===", sql, sqlParams, rows)
            if (rows && rows.length) {
                let id = rows[0]["id"];
                let newScore = score + rows[0]["score"];
                let newWintimes = wintimes + rows[0]["wintimes"];
                let newGametimes = gametimes + rows[0]["gametimes"];
                sql = "update t_club_top set `score`=?, `wintimes`=?, `gametimes`=?  where `id`=? ";
                sqlParams = [newScore, newWintimes, newGametimes, id];
                yield client.queryPromise(sql, sqlParams);
            }
            else {
                sql = "insert into t_club_top(clubid,userid,gametimes,wintimes,score, date) values(?,?,?,?,?,?)";
                sqlParams = [clubid, userid, gametimes, wintimes, score, dateStr];
                yield client.queryPromise(sql, sqlParams);
            }
        });
    }
    static updateTop(clubid, userid, key, value, dateStr) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "UPDATE t_club_top SET `" + key + "` = ? WHERE clubid = ? and userid=? and `date`=?";
            let sqlParams = [value, clubid, userid, dateStr];
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_club_top(clubid, topType, dateStr, page_size, cur_page) {
        return __awaiter(this, void 0, void 0, function* () {
            let orderStr = "gametimes";
            if (topType == ClubConfig_1.ClubConfig.Top_Type.gametimes) {
                orderStr = "gametimes";
            }
            else if (topType == ClubConfig_1.ClubConfig.Top_Type.score) {
                orderStr = "score";
            }
            else if (topType == ClubConfig_1.ClubConfig.Top_Type.wintimes) {
                orderStr = "wintimes";
            }
            let startIndex = (cur_page - 1) * page_size;
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from v_club_top where clubid=? and date=?  order by " + orderStr + " desc limit ?,?";
            let sqlParams = [clubid, dateStr, startIndex, page_size];
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_club_top==", sql, sqlParams);
            if (rows && rows.length) {
                return rows;
            }
            return [];
        });
    }
    static get_club_top_one(clubid, userid, dateStr) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from v_club_top where clubid=? and userid=? and date=? ";
            let sqlParams = [clubid, userid, dateStr];
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_club_top_one==", sql, sqlParams);
            if (rows && rows.length) {
                return rows;
            }
            return [];
        });
    }
    static get_club_top_totalnum(clubid, dateStr) {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select count(*) from t_club_top where clubid=? and date=?";
            let sqlParams = [clubid, dateStr];
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("get_club_top_totalnum==", clubid, rows);
            if (rows) {
                return rows[0]["count(*)"];
            }
            return 0;
        });
    }
    static clear_club_top(delete_time) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "delete from t_club_top where date < ?";
            let sqlParams = [DateUtil_1.default.formatDateStr(delete_time)];
            yield client.queryPromise(sql, sqlParams);
        });
    }
}
exports.ClubTopDao = ClubTopDao;
ClubTopDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
